﻿using System;
using System.IO;
using System.Text;
using System.Web;
using EmptyWallGallery.Core;

namespace EmptyWallGallery.FrontEnd.Services
{
    /// <summary>
    /// Summary description for Dashboard
    /// </summary>
    public class Dashboard : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var action = context.Request["action"];
            switch (action)
            {
                case "DeleteAnswer":
                    {
                        var answerId = context.Request["AnswerId"];
                        var questionId = context.Request["QuestionId"];
                        if (Core.Utilities.Common.Instance.IsNumeric(answerId))
                        {
                            Core.BLL.Answers.Instance.Delete(Convert.ToInt32(answerId));
                            Core.BLL.Questions.Instance.WriteQuestion(int.Parse(questionId));
                            Core.BLL.Questions.Instance.WriteVote();
                            context.Response.Write("1");
                        }
                        else
                        {
                            context.Response.Write("0");
                        }
                    }
                    break;
                case "DeleteFileAttach":
                    {
                        var fileId = context.Request["FileId"];
                        context.Response.Write(Core.BLL.AttachFiles.Instance.Delete(fileId) ? "1" : "0");
                        //context.Response.Write(Core.BLL.AttachFiles.Instance.Delete(fileId));
                    }
                    break;
                case "GetFileTyGia":
                    using (var sReader = new StreamReader(Configs.FolderSaveData + "tygia.xml", Encoding.UTF8))
                    {
                        context.Response.ContentType = "text/xml";
                        context.Response.Write(sReader.ReadToEnd());
                        sReader.Close();
                    }
                    break;
                case "GetFileHangHoa":
                    using (var sReader = new StreamReader(Configs.FolderSaveData + "hanghoa.xml", Encoding.UTF8))
                    {
                        context.Response.ContentType = "text/xml";
                        context.Response.Write(sReader.ReadToEnd());
                        sReader.Close();
                    }
                    break;
                case "GetFileChiSo":
                    using (var sReader = new StreamReader(Configs.FolderSaveData + "chiso.xml", Encoding.UTF8))
                    {
                        context.Response.ContentType = "text/xml";
                        context.Response.Write(sReader.ReadToEnd());
                        sReader.Close();
                    }
                    break;
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}